package com.fuhailiu.opengl.utils;

import android.content.Context;
import android.util.TypedValue;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

public class ResourceUtil {

	private static TypedValue mTmpValue = new TypedValue();

	public static Animation loadAnim(Context context, int id) {
		if (null == context) {
			return null;
		}
		return AnimationUtils.loadAnimation(context, id);
	}

	public static int getColor(Context context, int resId) {
		if (null == context || 0 == resId) {
			return 0;
		}
		return context.getResources().getColor(resId);
	}

	public static String getString(Context context, int resId) {
		if (null == context || 0 == resId) {
			return null;
		}
		return context.getResources().getString(resId);
	}

	public static String[] getStringArray(Context context, int[] resIds) {
		if (null == context || null == resIds) {
			return null;
		}
		int len = resIds.length;
		String[] result = new String[len];
		for (int i = 0; i < len; i++) {
			result[i] = getString(context, resIds[i]);
		}
		return result;
	}

	public static String formatString(Context context, int resId, Object... args) {
		return formatString(getString(context, resId), args);
	}

	public static String formatString(String string, Object... args) {
		if (null == string || null == args) {
			return null;
		}
		return String.format(string, args);
	}

	public static String mergeString(Context context, Object... args) {
		if (null == context || null == args) {
			return null;
		}
		StringBuffer sb = new StringBuffer();
		for (Object obj : args) {
			if (null == obj) {
				continue;
			}
			if (obj instanceof Integer) {
				sb.append(getString(context, (Integer) obj));
			} else if (obj instanceof String) {
				sb.append((String) obj);
			} else if (obj instanceof StringBuffer) {
				sb.append((StringBuffer) obj);
			} else if (obj instanceof StringBuilder) {
				sb.append((StringBuilder) obj);
			} else {
				sb.append(obj);
			}
		}
		return sb.toString();
	}

	public static int getValueFromXML(Context context, int id) {
		synchronized (mTmpValue) {
			TypedValue value = mTmpValue;
			context.getResources().getValue(id, value, true);
			return (int) TypedValue.complexToFloat(value.data);
		}
	}

	public static int getPixValueFromDimenXML(Context context, int resId) {
		if (0 == resId) {
			return 0;
		}
		return context.getResources().getDimensionPixelOffset(resId);
	}
}
